var mahjongSprites = [];

cc.Class({
    extends: cc.Component,

    properties: {
        leftAtlas: {
            default: null,
            type: cc.SpriteAtlas
        },
        rightAtlas: {
            default: null,
            type: cc.SpriteAtlas
        },

        bottomAtlas: {
            default: null,
            type: cc.SpriteAtlas
        },

        bottomFoldAtlas: {
            default: null,
            type: cc.SpriteAtlas
        },

        pengPrefabSelf: {
            default: null,
            type: cc.Prefab
        },

        pengPrefabLeft: {
            default: null,
            type: cc.Prefab
        },

        emptyAtlas: {
            default: null,
            type: cc.SpriteAtlas
        },
        holdsEmpty: {
            default: [],
            type: [cc.SpriteFrame]
        },
        _sides: null,
        _pres: null,
        _foldPres: null,
    },

    onLoad: function () {
        if (cc.vv == null) {
            return;
        }
        this._sides = ["myself", "right", "up", "left"];
        this._pres = ["M_", "R_", "B_", "L_"];
        this._foldPres = ["B_", "R_", "B_", "L_"];
        cc.vv.mahjongmgr = this;
        //筒
        for (var i = 1; i < 10; ++i) {
            mahjongSprites.push("dot_" + i);
        }

        //条
        for (var i = 1; i < 10; ++i) {
            mahjongSprites.push("bamboo_" + i);
        }

        //万
        for (var i = 1; i < 10; ++i) {
            mahjongSprites.push("character_" + i);
        }


        //东西南北风
        mahjongSprites.push("wind_east");
        mahjongSprites.push("wind_west");
        mahjongSprites.push("wind_south");
        mahjongSprites.push("wind_north");

        //中、发、白
        mahjongSprites.push("red");
        mahjongSprites.push("green");
        mahjongSprites.push("white");
    },

    getMahjongSpriteByID: function (id) {
        return mahjongSprites[id];
    },
    getMahjongType: function (id) {
        if (id >= 0 && id < 9) {
            return 0;
        }
        else if (id >= 9 && id < 18) {
            return 1;
        }
        else if (id >= 18 && id < 27) {
            return 2;
        }
        else if (id >= 27 && id < 34) {
            return 3;
        }
    },

    getSpriteFrameByMJID: function (pre, mjid) {
        var spriteFrameName = this.getMahjongSpriteByID(mjid);
        spriteFrameName = pre + spriteFrameName;

        if (pre == "M_") {
            return this.bottomAtlas.getSpriteFrame(spriteFrameName);
        }
        else if (pre == "B_") {
            return this.bottomFoldAtlas.getSpriteFrame(spriteFrameName);
        }
        else if (pre == "L_") {
            return this.leftAtlas.getSpriteFrame(spriteFrameName);
        }
        else if (pre == "R_") {
            return this.rightAtlas.getSpriteFrame(spriteFrameName);
        }
    },

    getAudioURLByMJID: function (id, index) {
        var isnn = cc.vv.common.onSex(index);
        return isnn + "/mj/mj_" + id + ".mp3";
    },

    getEmptySpriteFrame: function (side) {
        if (side == "up") {
            return this.emptyAtlas.getSpriteFrame("e_mj_b_up");
        }
        else if (side == "myself") {
            return this.emptyAtlas.getSpriteFrame("e_mj_b_bottom");
        }
        else if (side == "left") {
            return this.emptyAtlas.getSpriteFrame("e_mj_b_left");
        }
        else if (side == "right") {
            return this.emptyAtlas.getSpriteFrame("e_mj_b_right");
        }
    },

    getHoldsEmptySpriteFrame: function (side) {
        if (side == "up") {
            return this.emptyAtlas.getSpriteFrame("e_mj_up");
        }
        else if (side == "myself") {
            return null;
        }
        else if (side == "left") {
            return this.emptyAtlas.getSpriteFrame("e_mj_left");
        }
        else if (side == "right") {
            return this.emptyAtlas.getSpriteFrame("e_mj_right");
        }
    },
    sortMJ: function (mahjongs) {
        var self = this;

        mahjongs.sort(function (a, b) {
            return a - b;
        });

        for (var i = 0; i < mahjongs.length; ++i) {
            if (mahjongs[i] == cc.vv.gameNetMgr.useful) {
                mahjongs.splice(i, 1);
                mahjongs.unshift(cc.vv.gameNetMgr.useful);
            }
        }
    },

    getSide: function (localIndex) {
        return this._sides[localIndex];
    },

    getPre: function (localIndex) {
        return this._pres[localIndex];
    },

    getFoldPre: function (localIndex) {
        return this._foldPres[localIndex];
    }
});
